package {{.pkg}}

import (
	"context"
	"encoding/json"
	"fmt"

	"orchestrator/internal/infra/filter"
	"orchestrator/internal/infra/database/mysql/po"

	"github.com/osamingo/jsonrpc/v2"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/core/logc"
)

type {{.upperStartCamelObject}}DeleteParams struct {
	{{.upperStartCamelPrimaryKey}} {{.dataType}}
	*TxID
}

type {{.upperStartCamelObject}}DeleteResult struct {
}

type {{.upperStartCamelObject}}DeleteFunc struct {
	Db sqlx.SqlConn
	Repo po.{{.upperStartCamelObject}}Model
	{{.upperStartCamelObject}}DeleteParams
	{{.upperStartCamelObject}}DeleteResult
}

func (t *{{.upperStartCamelObject}}DeleteFunc) Name() string {
	return fmt.Sprintf("%s.%s", "{{.table}}", "Delete")
}

func (t *{{.upperStartCamelObject}}DeleteFunc) ServeJSONRPC(c context.Context, params *json.RawMessage) (result any, err *jsonrpc.Error) {
	logc.Infof(c, "%s( %v )", t.Name(), *params)

	p := []{{.upperStartCamelObject}}DeleteParams{}
	ppp := {{.upperStartCamelObject}}DeleteParams{}
	tx := {{.upperStartCamelObject}}DeleteParams{TxID: &TxID{}}
	p = append(p, ppp)
	p = append(p, tx)

	err1 := json.Unmarshal(*params, &p)
	if err1 != nil {
		return nil, &jsonrpc.Error{
			Code:    jsonrpc.ErrorCodeInvalidRequest,
			Message: err1.Error(),
		}
	}

	// 使用 json.MarshalIndent() 函数格式化输出
	pj, err1 := json.MarshalIndent(p, "", "  ")
	if err1 != nil {
		fmt.Println("Error:", err1)
		return nil, &jsonrpc.Error{
			Code:    jsonrpc.ErrorCodeInvalidRequest,
			Message: err1.Error(),
		}
	}

	fmt.Printf("%s:  %s\n", t.Name(), pj)

	return nil, nil
}

func (t *{{.upperStartCamelObject}}DeleteFunc) Params() any {
	return t.{{.upperStartCamelObject}}DeleteParams
}

func (t *{{.upperStartCamelObject}}DeleteFunc) Result() any {
	return t.{{.upperStartCamelObject}}DeleteResult
}



type {{.upperStartCamelObject}}DeleteByFilterParams struct {
	*filter.QueryRequest
	*TxID
}

type {{.upperStartCamelObject}}DeleteByFilterResult struct {
}

type {{.upperStartCamelObject}}DeleteByFilterFunc struct {
	Db sqlx.SqlConn
	Repo po.{{.upperStartCamelObject}}Model
	{{.upperStartCamelObject}}DeleteByFilterParams
	{{.upperStartCamelObject}}DeleteByFilterResult
}

func (t *{{.upperStartCamelObject}}DeleteByFilterFunc) Name() string {
	return fmt.Sprintf("%s.%s", "{{.table}}", "DeleteByFilter")
}

func (t *{{.upperStartCamelObject}}DeleteByFilterFunc) ServeJSONRPC(c context.Context, params *json.RawMessage) (result any, err *jsonrpc.Error) {
	logc.Infof(c, "%s( %s )", t.Name(), *params)

	d := filter.NewAndFilter()
	p := filter.NewQueryRequest(d, &filter.Group{}, []*filter.Sort{}, filter.NewPagination(0, 10))

	pl := []{{.upperStartCamelObject}}DeleteByFilterParams{}
	ppp := {{.upperStartCamelObject}}DeleteByFilterParams{QueryRequest: p}
	tx := {{.upperStartCamelObject}}DeleteByFilterParams{TxID: &TxID{}}
	pl = append(pl, ppp)
	pl = append(pl, tx)

	err1 := json.Unmarshal(*params, &pl)
	if err1 != nil {
		return nil, &jsonrpc.Error{
			Code:    jsonrpc.ErrorCodeInvalidRequest,
			Message: err1.Error(),
		}
	}

	// 使用 json.MarshalIndent() 函数格式化输出
	pj, err1 := json.MarshalIndent(pl, "", "  ")
	if err1 != nil {
		fmt.Println("Error:", err1)
		return nil, &jsonrpc.Error{
			Code:    jsonrpc.ErrorCodeInvalidRequest,
			Message: err1.Error(),
		}
	}

	fmt.Printf("%s:  %s\n", t.Name(), pj)

	r := {{.upperStartCamelObject}}DeleteByFilterResult{}
	return r, nil
}

func (t *{{.upperStartCamelObject}}DeleteByFilterFunc) Params() any {
	return t.{{.upperStartCamelObject}}DeleteByFilterParams
}

func (t *{{.upperStartCamelObject}}DeleteByFilterFunc) Result() any {
	return t.{{.upperStartCamelObject}}DeleteByFilterResult
}
